#include <bits/stdc++.h>

using namespace std;
struct ListNode {
    int val;
    ListNode* next;
    ListNode()
        : val(0), next(nullptr) {}
    ListNode(int x)
        : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next)
        : val(x), next(next) {}
};
class Solution {
   public:
    ListNode* removeNodes(ListNode* head) {
        vector<int> v;
        for (ListNode* p = head; p != nullptr; p = p->next){
            v.push_back(p->val);
        }
        int m = 0;
        for (int i = v.size()- 1; i >= 0; i--)
        {
            if (v[i] >= m){
                m = v[i];
            }
            else{
                v.erase(v.begin() + i);
            }
        }
        ListNode* ret = new ListNode(v[0]);
        ListNode* cur = ret;
        for (int i = 1; i < v.size(); i++){
            cur->next = new ListNode(v[i]);
            cur = cur->next;
        }
        cur->next = nullptr;
        delete head;
        return ret;
    }
};